angular.module('app.event')
    .controller('eventCtrl', ['$scope', '$filter','$location', '$modal', 'toaster', 'eventService',
        function ($scope, $filter,$location, $modal, toaster, eventService) {
            $scope.events = [];
            $scope.search;
            $scope.pager = {
                pageNo: 1,
                pageSize: 10,
                totalItems: 0
            };

            $scope.startTime;
            $scope.endTime;
            $scope.options = {
                allowInvalid: false,
                timezone: 'CCT'
            };

            $scope.searchEvent = function () {
                var query = {};
                query.search = $scope.search;
                query.pageNo = $scope.pager.pageNo;
                query.pageSize = $scope.pager.pageSize;
                query.startTime =$filter('date')($scope.startTime,'yyyy-MM-dd HH:mm:ss');
                query.endTime = $filter('date')($scope.endTime,'yyyy-MM-dd HH:mm:ss');
                eventService.eventList(query).then(function (res) {
                    $scope.events = res.data.data;
                    $scope.pager.totalItems = res.data.totalItems;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };
            $scope.searchEvent();

            $scope.addEvent = function(){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/event/add_event.html',
                    controller: 'eventModelCtrl',
                    size: 'md',
                    resolve: {
                        eventDetail: function () {
                            return {};
                        }
                    }
                });
                modalInstance.result.then(function (eventDetail) {
                    $scope.searchEvent();
                });
            };

            $scope.update = function(e){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/event/add_event.html',
                    controller: 'eventModelCtrl',
                    size: 'md',
                    resolve: {
                        eventDetail: function () {
                            return e;
                        }
                    }
                });
                modalInstance.result.then(function (eventDetail) {
                    $scope.searchEvent();
                });
            };

            $scope.delete = function(e){
                var modalInstance = $modal.open({
                    animation: true,
                    templateUrl: 'partials/confirm/confirm.html',
                    controller: 'confirmCtrl',
                    size: 'sm',
                    resolve: {
                        content: function () {
                            return "确认删除活动?";
                        }
                    }
                });
                modalInstance.result.then(function () {
                    eventService.deleteEvent(e).then(function (res) {
                        toaster.pop("success", "成功", "删除活动成功");
                        $scope.searchEvent();
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "删除活动失败")
                    });
                });
            };
        }])

    .controller('eventModelCtrl', ['$scope', '$filter','$location', '$modal','$modalInstance',
        'toaster', 'eventService','eventDetail',
        function ($scope, $filter,$location, $modal,$modalInstance, toaster, eventService,eventDetail) {
            $scope.eventDetail = eventDetail;
            $scope.title = "添加活动";

            $scope.startTime;
            $scope.endTime;
            $scope.options = {
                allowInvalid: false,
                timezone: 'CCT'
            };

            if($scope.eventDetail.id){
                $scope.title = "编辑活动";
                $scope.startTime = $scope.eventDetail.startTime
                $scope.endTime  = $scope.eventDetail.endTime;
            }

            $scope.save = function(){
                $scope.eventDetail.startTime =$scope.startTime;
                $scope.eventDetail.endTime = $scope.endTime;
                if($scope.eventDetail.id){
                    eventService.updateEvent($scope.eventDetail).then(function (res) {
                        $modalInstance.close($scope.eventDetail);
                        toaster.pop("success", "成功", "添加活动成功")
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "添加活动失败")
                    });
                }else{
                    eventService.addEvent($scope.eventDetail).then(function (res) {
                        $modalInstance.close($scope.eventDetail);
                        toaster.pop("success", "成功", "添加活动成功")
                    }, function (res) {
                        toaster.pop("error", "错误", res.errMsg || "添加活动失败")
                    });
                }
            };
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        }]);
